/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UploadDialog.java
 *
 * Created on Feb 10, 2010, 1:03:05 PM
 */

package com.googlecode.mindlistening.gui;

import com.googlecode.mindlistening.AudioRecordingState;
import com.googlecode.mindlistening.ConversationController;
import com.googlecode.mindlistening.DocumentStorage;
import com.googlecode.mindlistening.PropertiesStorage;
import com.googlecode.mindlistening.UploadListener;
import com.googlecode.mindlistening.UploadState;
import java.awt.BorderLayout;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import org.apache.commons.lang.StringUtils;
import org.jdesktop.swingx.JXErrorPane;
import sun.print.resources.serviceui_ja;

/**
 *
 * @author ryan
 */
public class UploadDialog extends javax.swing.JDialog implements UploadListener {

    private ConversationController controller;
    private PropertiesStorage propStorage;
    private DocumentStorage docStorage;



    /** Creates new form UploadDialog */
    public UploadDialog(ConversationController controller, final DocumentStorage docStorage, final PropertiesStorage propStorage, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();


        this.controller = controller;
        this.propStorage = propStorage;
        this.docStorage = docStorage;
        this.controller.addUploadListener(this);

        // set the server location.
        String location = docStorage.getServerUrl();
        if (location == null) {
            location = propStorage.loadProperty("uploadServerUrl");
        }
        if (location == null) {
            greenbridgeServer.setEditable(true);
        }
        greenbridgeServer.setText(location);
    }
   public void uploadChange(UploadState state) {
        if (state.getUploadURL() != null) {
            uploadComplete(state.getUploadURL());
        } else {
            this.overallProgressBar.setValue(state.getOverallPercentComplete());
            this.taskProgressBar.setString(state.getMinorTaskName());
            this.taskProgressBar.setStringPainted(true);
            taskProgressBar.setIndeterminate(state.isMinorTaskIndeterminate());
            if (!state.isMinorTaskIndeterminate()) {
                this.taskProgressBar.setValue(state.getMinorTaskPercentComplete());
            }


        }
    }
    private void uploadComplete(String uploadURL) {
        uploadURLEditorPane.setEnabled(true);
        uploadURLEditorPane.setText(createUploadHtml(uploadURL));
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        uploadPanel = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        imageHelpLabel = new javax.swing.JLabel();
        imagesTextField = new javax.swing.JTextField();
        imageFolderButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        yesPictures = new javax.swing.JRadioButton();
        noPictures = new javax.swing.JRadioButton();
        greenbridgeServer = new javax.swing.JTextField();
        uploadButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        taskProgressBar = new javax.swing.JProgressBar();
        jLabel9 = new javax.swing.JLabel();
        overallProgressBar = new javax.swing.JProgressBar();
        jScrollPane1 = new javax.swing.JScrollPane();
        uploadURLEditorPane = new javax.swing.JEditorPane();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        uploadPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Upload Conversation"));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("Greenbridge Server");

        imageHelpLabel.setText(" ");
        imageHelpLabel.setToolTipText("A directory that contains images to upload. Images where the date and time taken are between the conversation start and end will be uploaded.");

        imagesTextField.setEditable(false);
        imagesTextField.setToolTipText("A directory that contains images to upload. Images where the date and time taken are between the conversation start and end will be uploaded.");

        imageFolderButton.setText("...");
        imageFolderButton.setToolTipText("A directory that contains images to upload. Images where the date and time taken are between the conversation start and end will be uploaded.");
        imageFolderButton.setEnabled(false);
        imageFolderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                imageFolderButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Do you have pictures you have taken during this conversation?");

        buttonGroup1.add(yesPictures);
        yesPictures.setText("yes");
        yesPictures.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                yesPicturesActionPerformed(evt);
            }
        });

        buttonGroup1.add(noPictures);
        noPictures.setSelected(true);
        noPictures.setText("no");
        noPictures.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                noPicturesActionPerformed(evt);
            }
        });

        greenbridgeServer.setEditable(false);
        greenbridgeServer.setText("http://greenbridge.office-on-the.net/wikid");

        uploadButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/server_add.png"))); // NOI18N
        uploadButton.setText("Upload");
        uploadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uploadButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Cancel");
        jButton1.setEnabled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout uploadPanelLayout = new javax.swing.GroupLayout(uploadPanel);
        uploadPanel.setLayout(uploadPanelLayout);
        uploadPanelLayout.setHorizontalGroup(
            uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(uploadPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(uploadPanelLayout.createSequentialGroup()
                        .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(uploadPanelLayout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(noPictures)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(yesPictures)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(imagesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 465, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(uploadPanelLayout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(uploadPanelLayout.createSequentialGroup()
                                        .addComponent(uploadButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton1))
                                    .addComponent(greenbridgeServer, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(uploadPanelLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                                .addComponent(imageHelpLabel)
                                .addContainerGap())
                            .addGroup(uploadPanelLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(imageFolderButton)
                                .addContainerGap())))
                    .addGroup(uploadPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap(318, Short.MAX_VALUE))))
        );
        uploadPanelLayout.setVerticalGroup(
            uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(uploadPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(uploadPanelLayout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(imageHelpLabel))
                    .addGroup(uploadPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(noPictures)
                            .addComponent(yesPictures)
                            .addComponent(imagesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(imageFolderButton))
                        .addGap(18, 18, 18)
                        .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(greenbridgeServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(uploadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(uploadButton)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Progress"));
        jPanel1.setEnabled(false);

        jLabel8.setText("Task");
        jLabel8.setEnabled(false);

        taskProgressBar.setEnabled(false);

        jLabel9.setText("Overall");
        jLabel9.setEnabled(false);

        overallProgressBar.setEnabled(false);

        jScrollPane1.setBorder(null);

        uploadURLEditorPane.setContentType("text/html");
        uploadURLEditorPane.setEditable(false);
        jScrollPane1.setViewportView(uploadURLEditorPane);

        jLabel7.setText("URL");
        jLabel7.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(overallProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
                            .addComponent(taskProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(13, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(taskProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(overallProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(uploadPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(uploadPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void uploadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uploadButtonActionPerformed
        uploadButton.setEnabled(false);
        new Uploader().execute();
}//GEN-LAST:event_uploadButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        this.dispose();
    
    }//GEN-LAST:event_jButton1ActionPerformed



    private void imageFolderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_imageFolderButtonActionPerformed
        final JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = fc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            this.imagesTextField.setText(file.getAbsolutePath());
        }
    }//GEN-LAST:event_imageFolderButtonActionPerformed

    private void yesPicturesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_yesPicturesActionPerformed
        this.imageFolderButton.setEnabled(true);
        this.imagesTextField.setEditable(true);
    }//GEN-LAST:event_yesPicturesActionPerformed

    private void noPicturesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_noPicturesActionPerformed
        this.imageFolderButton.setEnabled(false);
        this.imagesTextField.setEditable(false);
    }//GEN-LAST:event_noPicturesActionPerformed
        private class Uploader extends SwingWorker<String,Void> {

            @Override
            protected String doInBackground()  {
                try {

                    File imagesDir = null;
                    if (yesPictures.isSelected() && StringUtils.isNotEmpty(imagesTextField.getText() )) {
                        imagesDir = new File(imagesTextField.getText());
                    }


                    Map properties = new HashMap(); // just empty for now
                    return controller.uploadConversation(greenbridgeServer.getText(), "couchdb", imagesDir, properties);
                } catch (final Exception e) {
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            overallProgressBar.setValue(0);
                            taskProgressBar.setStringPainted(false);
                            taskProgressBar.setIndeterminate(false);
                            taskProgressBar.setValue(0);
                            JXErrorPane.showDialog(e);
                        }
                    });
                    return e.getMessage();
                }
            }

            @Override
            protected void done() {
                try {
                    String location = get();
                    uploadURLEditorPane.setText(createUploadHtml(location));
                    uploadButton.setEnabled(true);
                    taskProgressBar.setStringPainted(false);
                    taskProgressBar.setIndeterminate(false);
                    taskProgressBar.setValue(100);
                    overallProgressBar.setValue(100);
                    propStorage.storeProperty("uploadServerUrl", greenbridgeServer.getText());
                } catch (Exception ex) {
                    
                }
            }


        }


    private String createUploadHtml(String url) {
        return "<html><a href=\"" + url + "\">" + url + "</a></html>";
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField greenbridgeServer;
    private javax.swing.JButton imageFolderButton;
    private javax.swing.JLabel imageHelpLabel;
    private javax.swing.JTextField imagesTextField;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton noPictures;
    private javax.swing.JProgressBar overallProgressBar;
    private javax.swing.JProgressBar taskProgressBar;
    private javax.swing.JButton uploadButton;
    private javax.swing.JPanel uploadPanel;
    private javax.swing.JEditorPane uploadURLEditorPane;
    private javax.swing.JRadioButton yesPictures;
    // End of variables declaration//GEN-END:variables

}
